import Vue from 'vue'

Vue.filter('price', (price, fixed=0)=>{
	if(!price || isNaN(price)){
		return 0
	}
	return fixed > 0 ? parseFloat(price).toFixed(fixed) : + parseFloat(price).toFixed(2);
})

Vue.filter('add', (rate)=>{
	if(!rate || isNaN(rate)){
		return 0
	}
	return rate > 0 ? '+'+rate : rate;
})

Vue.filter('noPassByMobile',(str)=>{
	if(null != str && str != undefined && str !=''){
		var pat=/(\d{3})\d*(\d{4})/;
		return str.replace(pat,'$1****$2');
	} else {
		return "";
	}
})
Vue.filter('srcDomain',(str)=>{
	if(null != str && str != undefined && str !=''){
		let ossapi='https://bendike.oss-cn-hangzhou.aliyuncs.com/';
		if(str.indexOf('bendike.oss') == -1){
			return ossapi+str
		}else{
			return str
		}
	}else{
		return ''
	}
	
})
function checkTime(i){
	if (i<10) {
		i = "0" + i;
	}
	return i;
}
Vue.filter('countDown',(lastTime)=>{
	var ts =lastTime;//剩余的毫秒数
	var showTime = '';
	// console.log(ts)
	if(ts>0){
		var dd = parseInt(ts  / 60 / 60 / 24, 10);//计算剩余的天数
		var hh = parseInt(ts  / 60 / 60 % 24, 10);//计算剩余的小时数
		var mm = parseInt(ts  / 60 % 60, 10);//计算剩余的分钟数
		var ss = parseInt(ts  % 60, 10);//计算剩余的秒数
		dd = checkTime(dd);
		hh = checkTime(hh);
		mm = checkTime(mm);
		ss = checkTime(ss);
		if(dd>0){
			showTime = `${dd}天:${hh}:${mm}:${ss}`
		}else{
			if(hh>0){
				showTime = `${hh}:${mm}:${ss}`
			}else{
				if(mm>0){
					showTime = `00:${mm}:${ss}`
				}else{
					if(ss>0){
						showTime = `00:00:${ss}`
					}
				}
			}
		}
	}else{
		showTime = 0
	}
	 return showTime;
})

